package net.freestu.sola.web.global;

import java.util.*;

public class AllPagesCounter {
	
	
	
	private Map themap;
	private static AllPagesCounter singleton;
	
	
	private AllPagesCounter(){
		
		themap=new HashMap();
	}
	
	synchronized public static AllPagesCounter getInstance(){
		
		 if(singleton==null){
			  singleton=new AllPagesCounter();
		  }
		  return singleton;
	}
	
	synchronized public void put(String name){
		
		PageCounter pc=new PageCounter(name);
		themap.put(name,pc);
	}
	
	synchronized public PageCounter get(String name){
		
		return (PageCounter)themap.get(name);
	}
	
	/**
	 * 得到所有的Page的数量。
	 * @return
	 */
	synchronized public int getAllPageNum(){
		
		Collection c =themap.values(); 
		
		Iterator it=c.iterator(); 
		int num=0;
		while(it.hasNext()){
			
			PageCounter pc=(PageCounter)it.next();
			int nn= pc.getPageNum();
			num+=nn;
		}
		return num;
	}
    
	
	public static void main(String[] args){
		
		AllPagesCounter apc=AllPagesCounter.getInstance();
		apc.put("cdut.edu.cn");
		apc.put("sina.com");
		apc.put("google.com");
		
		
		PageCounter pc=apc.get("cdut.edu.cn");
		pc.addOne();
		
		
		PageCounter pc2=apc.get("google.com");
		pc2.addOne();
		pc2.addOne();
		
		
		PageCounter pc3=apc.get("sina.com");
		pc3.addOne();
		pc3.addOne();
		pc3.addOne();
		//System.out.println(apc.get("cdut.edu.cn").getPageNum());
		//System.out.println(apc.get("google.com").getPageNum());
		//System.out.println(apc.get("sina.com").getPageNum());
		System.out.println(apc.getAllPageNum());
	}
}
